function delayDo(ms,func,...)
	delayDo__(ms,func,{...})
end



sno = 0
function testSend(addr,len)
	assert(len>=4,"发包长度至少等于4。");
	pkg={0x20,sno&0xff,(sno>>8)&0xff}
	for i=4,len,1 do
		pkg[i] = math.random(0,255);
	end
	sno = sno+1
	pkg.addr = addr
	pkg.port = 48
	send__(pkg)
	print("发出了 #%d 数据包。",pkg[2]+(pkg[3]<<8))
end

function __receive(r_pkg)
	if r_pkg[1]== 0x20 then
		s_pkg = {0x40,r_pkg[2],r_pkg[3]}
		s_pkg.addr = r_pkg.addr
		s_pkg.port = 48
		send__(s_pkg);
	elseif r_pkg[1]== 0x40 then
		print("包确认收到%d。",r_pkg[2]+(r_pkg[3]<<8))
	else 	
		body = ""
		for i=1,#r_pkg do			
			body= body..string.format("%02X ", r_pkg[i])
		end	
		print("收到了 %d Byte数据： %04X->%d: %q",#r_pkg,r_pkg.addr, r_pkg.port,body)
	end
end
	
function mutTestOnce(cfg)
	testSend(cfg.addr,cfg.send_len)
	if cfg.count>=1 then
		cfg.count = cfg.count-1;
		delayDo(cfg.delay,mutTestOnce,cfg)
	end
end
function mutTest(addr,count,send_len)
	cfg = {delay= 1000,addr = addr,count = count,send_len = send_len}
	print("向%04X 发送%d次数据.",addr,count)
	mutTestOnce(cfg)
end	

random = {min = 50,max = 2000,send_len = 5}

function randomDelaySendOnce(addr,count)
	testSend(addr,random.send_len)
	if count>1 then
		delayDo(math.random(random.min,random.max),randomDelaySendOnce,addr,count-1)
	end
end

function randomTest(addr,count)
	print("随机延时测试：向%04X 发送%d次数据",addr,count)
	randomDelaySendOnce(addr,count)
end

--randomTest(0x000e,1000)

--mutTest(0x000e,10,10)